package com.xinqi.modules.course.chapter.convert;

import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.xinqi.common.base.enums.YesNoEnum;
import com.xinqi.common.mapstruct.CommonConvertConfig;
import com.xinqi.modules.course.chapter.domain.CourseChapterEntity;
import com.xinqi.modules.sys.textbook.dto.rsp.CloudTextbookCatalogResultDTO;
import org.mapstruct.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import java.util.Set;

/**
 * 课程章节转换器
 *
 * @author: yyzhen
 * @date: 2023/03/13
 */
@Mapper(config = CommonConvertConfig.class)
public abstract class CourseChapterConvert {
    @Lazy
    @Autowired
    private IdentifierGenerator identifierGenerator;

    public abstract CourseChapterEntity newEntity(CourseChapterEntity s);

    public CourseChapterEntity changeChapter(Long courseId, Long classId, Long pid, CloudTextbookCatalogResultDTO catalog, Set<Long> refIdSet) {

        CourseChapterEntity chapterEntity = new CourseChapterEntity();
        if (refIdSet.contains(catalog.getId())) {
            chapterEntity.setIsHasContent(YesNoEnum.YES.ordinal());
        }

        chapterEntity.setId(identifierGenerator.nextId(null).longValue());
        chapterEntity.setCourseId(courseId);
        chapterEntity.setClassId(classId);
        chapterEntity.setPid(pid);
        chapterEntity.setName(catalog.getName());
        chapterEntity.setIsCloudTextbookCatalog(YesNoEnum.YES.ordinal());
        chapterEntity.setSort(catalog.getSort());
        return chapterEntity;
    }

}
